package first.codeTop.meituan;

import java.util.HashMap;
import java.util.Stack;

public class Leetcode20 {
    // 有效的括号
    // 输入：s = "()[]{}"
    // 输出：true
    public static void main(String[] args) {

    }

    public static boolean isValid(String s) {
        if (s.length() % 2 == 1) {
            return false;
        }
        HashMap<Character, Character> map = new HashMap<>();
        map.put('}', '{');
        map.put(']', '[');
        map.put(')', '(');
        Stack<Character> stack = new Stack<>();
        for (int i=0; i<s.length(); i++) {
            if (map.containsKey(s.charAt(i))) {
                if (stack.isEmpty() || map.get(s.charAt(i)) != stack.peek()) {
                    return false;
                }
                // 如果匹配成功就弹出
                stack.pop();
            } else {
                stack.push(s.charAt(i));
            }
        }
        return stack.isEmpty();
    }
}
